﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Having.Site.Base
{
    /// <summary>
    /// 平均分
    /// </summary>
    public class Mean
    {
        /// <summary>
        /// 数值
        /// </summary>
        public decimal Ite { set; get; }
        /// <summary>
        /// 获取平均分的数值
        /// </summary>
        public decimal MeanIte { set; get; }
        /// <summary>
        /// 其他备注
        /// </summary>
        public string Mark { set; get; }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="_Ite">数值</param>
        /// <param name="_Mark">备注</param>
        public Mean(decimal _Ite, string _Mark = "")
        {
            this.Ite = _Ite;
            this.Mark = _Mark;
        }

        /// <summary>
        /// 调用平均分逻辑
        /// </summary>
        /// <param name="dm">要平均分的数值</param>
        /// <param name="list">数据</param>
        /// <param name="sp">保留小数点位数</param>
        public static void MeanPear(decimal dm, ref List<Mean> list, int sp = 2,Action ac=null)
        {
            var sm = list.Sum(m => m.Ite);
            var dm1 = dm;
            list.ForEach(m => { 
                var m1 = Math.Round(dm * (m.Ite / sm), sp);
                if (m1 < Convert.ToDecimal(Math.Round(Math.Pow(0.1, sp),sp)))
                {
                    m.MeanIte = 0;
                }
                else
                {
                    if (dm1 < m1) {
                        m1 = dm1;
                    }
                    m.MeanIte = m1;
                    dm1 -= m1;
                    ac?.Invoke();
                }
            });
        }

    }
}
